package edu.kpi.pzks2.ui.simulation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import net.miginfocom.swing.MigLayout;
import edu.kpi.pzks2.simulator.results.ChannelHistoryBean;
import edu.kpi.pzks2.simulator.results.HistoryBean;
import edu.kpi.pzks2.simulator.results.ProcHistoryBean;
import edu.kpi.pzks2.simulator.results.TimeLine;
import edu.kpi.pzks2.ui.simulation.gantt.GanttTableModel;

@SuppressWarnings("serial")
public class SimulationPanel extends JPanel {
    private JTable tableProcessors;
    private JTable tableChannels;

    private final GanttTableModel procTableModel;
    private final GanttTableModel chanTableModel;
    
    public SimulationPanel() {
        setLayout(new MigLayout("", "[][grow]", "[][grow]"));

        JLabel lblProcessors = new JLabel("Processors");
        add(lblProcessors, "cell 0 0");

        JLabel lblChannels = new JLabel("Channels");
        add(lblChannels, "cell 1 0");

        procTableModel = new GanttTableModel();
        chanTableModel = new GanttTableModel();
        
        tableProcessors = new JTable(procTableModel);
        tableChannels = new JTable(chanTableModel);

        tableProcessors.setDefaultRenderer(HistoryBean.class, new CellRenderer());
        tableChannels.setDefaultRenderer(HistoryBean.class, new CellRenderer());
        
        tableProcessors.getColumnModel().getColumn(0).setMaxWidth(20);
        tableChannels.getColumnModel().getColumn(0).setMaxWidth(20);
        
//        final JScrollPane scrollBar = new JScrollPane(tableProcessors);
        final JPanel pan1 = new JPanel(new BorderLayout());
        pan1.add(tableProcessors, BorderLayout.CENTER);
        pan1.add(tableProcessors.getTableHeader(), BorderLayout.NORTH);
        add(pan1, "cell 0 1,grow");

//        JScrollPane scrollPane = new JScrollPane(tableChanels);
        final JPanel pan2 = new JPanel(new BorderLayout());
        pan2.add(tableChannels, BorderLayout.CENTER);
        pan2.add(tableChannels.getTableHeader(), BorderLayout.NORTH);
        add(pan2, "cell 1 1,grow");
    }

    public void setData(final List<TimeLine> procHistory, final List<TimeLine> chanHistory) {
        chanTableModel.setTimeLines(chanHistory);
        procTableModel.setTimeLines(procHistory);
        
        chanTableModel.fireTableStructureChanged();
        procTableModel.fireTableStructureChanged();
        
        tableProcessors.getColumnModel().getColumn(0).setMaxWidth(25);
        tableChannels.getColumnModel().getColumn(0).setMaxWidth(25);
        
        for (int i = 1; i < tableProcessors.getColumnModel().getColumnCount(); i++) {
            tableProcessors.getColumnModel().getColumn(i).setMaxWidth(40);
        }
        
        chanTableModel.fireTableDataChanged();
        procTableModel.fireTableDataChanged();
    }
    
    public static class CellRenderer extends JLabel implements TableCellRenderer {
        public CellRenderer() {
            super("", CENTER);
            setOpaque(true); //MUST do this for background to show up.
        }

        public Component getTableCellRendererComponent(
                JTable table,
                Object historyBean,
                boolean isSelected,
                boolean hasFocus,
                int row, int column) {
            if (historyBean != null) {
                setBackground(Color.GREEN);
                setText(historyBean.toString());
                
                if (historyBean instanceof ChannelHistoryBean) {
                    if (((ChannelHistoryBean) historyBean).isBothBusy()) {
                        setBackground(Color.YELLOW);
                    }
                }
                
                if (historyBean instanceof ProcHistoryBean) {
                    if (((ProcHistoryBean) historyBean).isWaiting()) {
                        setBackground(Color.LIGHT_GRAY);
                    }
                }
            } else {
                setBackground(Color.GRAY);
                setText("|");
            }

            return this;
        }
    }
}